package com.yaodian100.ec.order.web;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;

import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.Preparable;
import com.yaodian100.core.admin.utils.AdminHelper;
import com.yaodian100.core.common.exception.CoreException;
import com.yaodian100.ec.order.entity.Orders;
import com.yaodian100.ec.order.service.OrderService;
import com.yaodian100.ec.order.service.ServiceOrderService;

public class CancelOrderAction extends ActionSupport implements Preparable {
	private static final long serialVersionUID = 1L;

	@Autowired
	private OrderService orderService;
	@Autowired
	private ServiceOrderService serviceOrderService;

	private String orderId;
	private Orders order;
	private String memo;
	private String reason;
	private String description;

	@Override
	public void prepare() throws Exception {
		clearErrorsAndMessages();
	}

	public String toCancelOrder() throws Exception {
		this.order = orderService.get(orderId);
		return "toCancelOrder";
	}

	public String cancelOrder() {
		if (!AdminHelper.hasPermission("OTHER_CANCELORDER")) {
			addActionError("权限不正确");
			return "cancelOrder";
		}
		Orders order = orderService.get(orderId);
		try {
			if ("其它".equals(reason)) {
				reason = description;
			}
			String[] splitOrderId = StringUtils.split(order.getJoinOrders(), ",");
			for (String orderId : splitOrderId) {
				if (!orderId.equals(order.getOrderId())) {
					order = orderService.get(orderId);
				}
				this.orderService.cancelOrderBySystemUser(order, reason, memo);
			}
		} catch (CoreException e) {
			addActionError(e.getMessage());
		}
		return "cancelOrder";
	}

	public String revertOrder() {
		if (!AdminHelper.hasPermission("OTHER_CANCELORDER")) {
			addActionError("权限不正确");
			return "revertOrder";
		}
		try {
			this.orderService.revertOrder(orderId);
		} catch (CoreException e) {
			addActionError(e.getMessage());
		}
		return "revertOrder";
	}

	public OrderService getOrderService() {
		return orderService;
	}

	public void setOrderService(OrderService orderService) {
		this.orderService = orderService;
	}

	public String getOrderId() {
		return orderId;
	}

	public void setOrderId(String orderId) {
		this.orderId = orderId;
	}

	public Orders getOrder() {
		return order;
	}

	public void setOrder(Orders order) {
		this.order = order;
	}

	public ServiceOrderService getServiceOrderService() {
		return serviceOrderService;
	}

	public void setServiceOrderService(ServiceOrderService serviceOrderService) {
		this.serviceOrderService = serviceOrderService;
	}

	public String getMemo() {
		return memo;
	}

	public void setMemo(String memo) {
		this.memo = memo;
	}

	public String getReason() {
		return reason;
	}

	public void setReason(String reason) {
		this.reason = reason;
	}

	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

}
